package com.lf2.tool;

import com.lf2.Lf_Debug;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil
{
    public DateUtil() {}

    public static int getMonthesBetween(Date beginDate, Date endDate)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginDate);
        int month1 = calendar.get(1) * 12 + calendar.get(2);
        calendar.setTime(endDate);
        int month2 = calendar.get(1) * 12 + calendar.get(2);
        return month2 - month1 + 1;
    }

    public static int getMonthOfYear(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(2);
    }

    public static Date getDate(int year, int month, int day)
    {
        return getDate(year, month, day, 0, 0, 0);
    }

    public static Date getDate(int year, int month, int day, int hourOfDay, int minute, int second)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hourOfDay, minute, second);
        return calendar.getTime();
    }

    public static int getYear(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(1);
    }

    public static int getMonth(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(2);
    }

    public static int getDaysOfMonth(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(5);
    }

    public static long getDayOfMonth(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(5);
    }

    public static long getDaysBetween(Date beginDate, Date endDate)
    {
        return (endDate.getTime() - beginDate.getTime()) / 86400000L;
    }

    public static String getDate(Date date, String expectedFormat)
    {
        SimpleDateFormat format = new SimpleDateFormat(expectedFormat);
        return format.format(date);
    }

    public static int getDayOfWeek(Date date)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        return c.get(7);
    }

    public static Date getDate(String value, String format)
    {
        Date date = null;
        try 
        {
            date = new SimpleDateFormat(format).parse(value);
        }
        catch (ParseException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return date;
    }

    public static String getCurrentTime(String format)
    {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date curDate = new Date(System.currentTimeMillis());

        return df.format(curDate);
    }

    public static long getDaysBetween(String beginDay, String endDay, String format)
    {
        SimpleDateFormat df = new SimpleDateFormat(format);
        long to = 0L;
        try 
        {
            to = df.parse(beginDay).getTime();
        } 
        catch (ParseException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        long from = 0L;
        try 
        {
            from = df.parse(endDay).getTime();
        } 
        catch (ParseException e) {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }

        return (from - to) / 86400000L;
    }

    public static Calendar getCalendarOfInterval(Date nowDate, int days)
    {
        Calendar ca2 = Calendar.getInstance();
        ca2.setTime(nowDate);
        ca2.add(5, days);

        return ca2;
    }

    public static String getFormattedDate(String date, String oldFormat, String newFormat)
    {
        try
        {
            return new SimpleDateFormat(newFormat).format(new SimpleDateFormat(oldFormat).parse(date));
        } 
        catch (ParseException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return null;
    }

    public static String getFormattedTime(String time, String oldFormat, String newFormat)
    {
        try
        {
            return new SimpleDateFormat(newFormat).format(new SimpleDateFormat(oldFormat).parse(time));
        } 
        catch (ParseException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return null;
    }

    public static String getFormattedDateTime(String dataTime, String oldFormat, String newFormat)
    {
        try
        {
            return new SimpleDateFormat(newFormat).format(new SimpleDateFormat(oldFormat).parse(dataTime));
        } 
        catch (ParseException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return null;
    }

    public static long getSecondsBetween(String beginTime, String endTime, String format)
    {
        SimpleDateFormat df = new SimpleDateFormat(format);
        long to = 0L;
        try 
        {
            to = df.parse(beginTime).getTime();
        } 
        catch (ParseException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        long from = 0L;
        try 
        {
            from = df.parse(endTime).getTime();
        } 
        catch (ParseException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }

        return (from - to) / 1000L;
    }

    public static Date getDateBefore(Date d, int day)
    {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(5, now.get(5) - day);
        return now.getTime();
    }

    public static boolean compareDate(Date begin, Date end, int day)
    {
        Calendar cbegin = Calendar.getInstance();
        cbegin.setTime(begin);

        Calendar cend = Calendar.getInstance();
        cend.setTime(end);

        if (cend.get(5) - cbegin.get(5) <= day)
        {
            return true;
        }
        return false;
    }

    public static boolean compareDate(String date1, String date2, String format)
    {
        SimpleDateFormat df = new SimpleDateFormat(format);
        try
        {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() < dt2.getTime()) {
                return true;
            }
        } 
        catch (ParseException e) 
        {
            e.printStackTrace();
			Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return false;
    }
}
